/****************************************************************************************************************************************************

Replicate code for the paper: "The Challenge of Sustaining Effective Teaching: Spillovers, Fade-out, and the Cost-effectiveness of Teacher Development Programs" Economics of Education Review

Author: Jacobus Cilliers
Last Edited: January 12th 2022

Note: 
1. Define your own global for the data folder. 
2. There are three datasets:
 - Student-level data: "sustainability_paper"
 - Teacher-level data (grades 1 and 3): sustainability_paper_teacher_level
 - Teacher-level data (Grade 2): sustainability_paper_gr2_teacher_level
3. Read the paper for a detailed description of the data. 

****************************************************************************************************************************************************/


**********DEFINE SOME GLOBALS************************
clear
set more off

if c(username) == "jacob" {

global input "C:\Users\jacob\Dropbox\EGRS I Data\replicate_sustainability_paper\data"

}

*Controls
global BL_controls BL_score_A BL_score_B BL_score_C BL_score_D BL_score_E BL_score_F_compr BL_score_Fwords
global controls female_W2 female_miss age_imputed  age_mis P_ed2-P_ed6
global community_controls C2011_SP_wealth_index C2011_attendance district ANA_new		
	
global cohort1_controls  $BL_controls female_W2 female_miss    P_ed2 P_ed3 P_ed5 P_ed6 C2011_attendance //selected using backwards regression procedure
global gr3_controls Learner_Age  w4_female C2011_school_att_13_18yr mean_BL_score_B mean_BL_score_Fwords //selected using backwards regression procedure

global tch_controls	W4_TQ_yearsteach W4_TQ_learnerno W4_TQ_tage C2011_school_att_13_18yr mean_BL_score_B mean_BL_score_Fwords 


/***************************************************************************************
*** Table 1: Balance on on sample of schools where collected data for second cohort. ***
(NOTE: I copy the different balance tables together, and manually added number of observations as a final column)
****************************************************************************************/

	use "$input\sustainability_paper.dta", clear	 
	
	***Panel B: student level (directly treated)
	lab var BL_totscore_SD "Baseline performance"
	iebaltab age_best_W1and2 female_with_miss BL_totscore_SD if cohort == 1, ///
		grpvar(treatment) savet("$tables/balance_cohort1.tex") replace cov(i.strata) vce(cluster idschool) rowvarlabels control(0)   balmissreg(groupmean) onerow

	***Panel C: student level (post-treated)		
	iebaltab age_with_mis female_with_mis if cohort == 2 & female_with_mis ~= . & cohort == 2, ///
		grpvar(treatment) savet("$tables/balance_cohort2.tex") replace cov(i.strata) vce(cluster idschool) rowvarlabels   control(0)  balmissreg(groupmean) onerow

	***Panel A: school level
	keep if cohort == 1
	duplicates drop idschool, force
	lab var p_educ_secondorhigher "Secondary or higher"
	lab var C2011_SP_wealth_index "Wealth index"
	lab var W4_PQ_enroll_grade1 "Grade 1 enrollment (2018)"
	lab var W4_PQ_enroll_grade3 "Grade 3 enrollment (2018)"
	lab var W4_PQ_enroll_grade4 "Grade 4 enrollment (2018)"
	lab var early_grade_vacancy "Teacher vacancies (grades 1 to 3)"
	iebaltab speak_setswana p_educ_secondorhigher rural_dummy Q1 ANA_new C2011_SP_wealth_index  district W4_PQ_enroll_grade3 early_grade_vacancy, ///
		grpvar(treatment) savet("$tables/balance_grade3_school.tex") replace cov(i.strata) rowvarlabels control(0) balmissreg(groupmean) onerow

	***Panel D. Post-treated grade 3 teachers
	use "$input\sustainability_paper_teacher_level.dta", clear	
	lab var same_gr_sch_2017 "Taught grade 3 2017"
	keep if gr3_sample | gr1_sample
	lab var exposed "Previously exposed"
	
	iebaltab female W4_TQ_tage	W4_TQ_yearsteach W4_TQ_multigrade W4_TQ_learnerno recent_teacher same_gr_sch_2017 if gr3_sample, ///
			grpvar(treatment) savet("$tables/balance_teacher_gr3.tex") replace cov(i.strata) vce(cluster idschool) rowvarlabels  control(0) balmissreg(groupmean) onerow
			
	***Panel E. Post-treated grade 1 teachers		
	iebaltab W4_TQ_yearsteach W4_TQ_multigrade W4_TQ_learnerno recent_teacher same_gr_sch_2015 if gr1_sample, ///
			grpvar(treatment) savet("$tables/balance_teacher_gr1.tex") replace cov(i.strata) vce(cluster idschool) rowvarlabels  control(0) balmissreg(groupmean) onerow			
	
	
************************************************************************
* Table 2. Teacher-level outcomes
************************************************************************

	*** 2 Panel A: Grade 2 teachers, year of exposure ****	
use "$input\sustainability_paper_gr2_teacher_level.dta", replace	

**Create fake variables just to have place holders for the outcomes not measured for grade 2 teachers, but measured for others. 
gen lessonplans = 1
gen readers = 1
foreach var of varlist lessonplans readers W3_TQ_phonics_correct prop_correct  me_print writing_me  {
	qui reg `var' T1 T2 i.strata, cl(idschool) 
	qui test T1 = T2
	local p1 `r(p)'
	qui sum `var' if treat == 0 
	local m `r(mean)'
	eststo `var', addscalars(p1 `p1' m `m')	
}
	esttab 	 	lessonplans readers W3_TQ_phonics_correct  ///
				prop_correct  ///
				me_print  ///
				writing_me  ///
				using "$tables/teacher_level.tex", replace ///
				tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
				stats(N r2 p1 m,  fmt(0 3 3 3) labels("Observations" "R-squared" "P-value:Train=Coach" "Control mean")) ///	
				keep( T1 T2) ///
				collabels(none)	 ///
				mtitles("\shortstack{Lesson\\plans}" "\shortstack{Reading\\Booklets}" "Knowledge" "\shortstack{Teaching\\activities}"  /// 
				"\shortstack{Print\\richness}" "Writing") ///
				nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 		

	***Panel b: Grade 3  ***
	use "$input\sustainability_paper_teacher_level.dta", clear
	tab strata, gen(dum_strata)
	drop dum_strata10
	**Impute mean for the control variables that have some missing values. 
	foreach var of varlist W4_TQ_yearsteach W4_TQ_learnerno	 W4_TQ_tage {
		gen `var'_mis = `var' == .
		qui sum `var'
		replace `var' = `r(mean)' if `var' == .
	}
					   
foreach var of varlist use_lesson_plans_ngo vula_bula phonics_repeat_thrice overall_correct print_me writing_me {
	qui reg `var' T1 T2 $tch_controls i.strata if gr3_sample, cl(idschool) 
	qui test T1 = T2
	local p1 `r(p)'
	qui sum `var' if treat == 0 & gr3_sample
	local m `r(mean)'
	eststo gr3_`var', addscalars(p1 `p1' m `m')	
	}
		
	***Panel b: Grade 3  ***
	esttab 	 	gr3_use_lesson_plans_ngo gr3_vula_bula gr3_phonics_repeat_thrice ///
				gr3_overall_correct ///
				gr3_print_me ///
				gr3_writing_me ///
				using "$tables/teacher_level.tex", append ///
				tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
				stats(N r2 p1 m,  fmt(0 3 3) labels("Observations" "R-squared" "P-value:Train=Coach" "Control mean")) ///	
				keep( T1 T2) ///
				collabels(none)	 ///
				nomtitles nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 								

	***Panel C: Grade 1  ***

foreach var of varlist use_lesson_plans_ngo vula_bula phonics_repeat_thrice overall_correct print_me writing_me {
	qui reg `var' T1 T2 $tch_controls i.strata if gr1_sample == 1 , cl(idschool) 
	qui test T1 = T2
	local p1 `r(p)'
	qui sum `var' if treat == 0 & gr1_sample == 1 & same_gr_sch_2015 == 1
	local m `r(mean)'
	eststo gr1_`var', addscalars(p1 `p1' m `m')	
	}
	
	
	esttab 	 	gr1_use_lesson_plans_ngo gr1_vula_bula gr1_phonics_repeat_thrice ///
				gr1_overall_correct ///
				gr1_print_me ///
				gr1_writing_me ///
				using "$tables/teacher_level.tex", append ///
				tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
				stats(N r2 p1 m,  fmt(0 3 3) labels("Observations" "R-squared" "P-value:Train=Coach" "Control mean")) ///	
				keep( T1 T2) ///
				collabels(none)	 ///
				nomtitles nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 						

				

************************************************************************
* Table 3. Impacts on learning
************************************************************************
	***	Post-treated cohort
	use "$input\sustainability_paper.dta", clear
	keep if cohort == 2
	keep if W4_LT_gr3_totscore ~= .
	*Create dummies from strata indicator variable. I need to do this in order to interact with treatment assignment.  
	tab strata, gen(dum_strata)
	drop dum_strata10
	
	*Demean every control and interact with treatment. 
	foreach var of varlist $gr3_controls dum_str*   {
	qui sum `var' 
	gen z_`var' = `var' - `r(mean)' 
	forvalues i = 1/2 {
		gen z_`var'_x_T`i' = z_`var' * T`i'
	}	
	}	
	
	*Regressions for the post-treated cohort. We store the estimates output them after running the regressions on the directly-treated cohort 
	eststo clear
	foreach var of varlist W4_LT_gr3_totscore /// Post-treated cohort: Main outcome 
						Z_gr3* 		{ //Post-treated cohort: These are the consituent indicators. 
	reg `var' T1 T2 z*, cl(idschool)
	qui test T1 = T2
	local p1 `r(p)'
	qui sum `var' if treat == 0 
	local m `r(mean)'
	eststo `var', addscalars(p1 `p1' m `m')
	}	
	
		
	***	Directly treated cohort. 
	use "$input\sustainability_paper.dta", clear
	keep if cohort == 1
	*Regression for Table 3. 
	keep if W3_LT_totscore ~= .
	tab strata, gen(dum_strata)
	drop dum_strata10
	**Grade 2
	foreach var of varlist  $cohort1_controls dum_str* {
	qui sum `var' 
	gen z_`var' = `var' - `r(mean)' 
	forvalues i = 1/2 {
		gen z_`var'_x_T`i' = z_`var' * T`i'
	}	
	}
	
	qui reg W3_LT_totscore T1 T2  z*, cl(idschool)
	qui test T1 = T2
	local p1 `r(p)'
	qui sum W3_LT_totscore if treat == 0 
	local m `r(mean)'
	eststo W3_LT_totscore, addscalars(p1 `p1' m `m')


	**Grade 1. We need to create a new set of interacted control variables, because the sample is slightly different, so the control means are slightly different for that sample. 
	cap drop z_*
	drop if W2_LT_totscore == .
	foreach var of varlist  $cohort1_controls  {
	qui sum `var' 
	gen z_`var' = `var' - `r(mean)' 
	forvalues i = 1/2 {
		gen z_`var'_x_T`i' = z_`var' * T`i'
	}	
	}
	
	qui reg W2_LT_totscore T1 T2  z_*, cl(idschool)
	qui test T1 = T2
	local p1 `r(p)'
	qui sum W3_LT_totscore if treat == 0 
	local m `r(mean)'
	eststo W2_LT_totscore, addscalars(p1 `p1' m `m')

	
	**Output table 3. 
	esttab 	 	W4_LT_gr3_totscore /// overall
				Z_gr3_la_tsk_2_tot		/// letter recognition
				Z_gr3_la_tsk_3_tot						/// word recognition
				Z_gr3_la_tsk_4a_tot	/// Paragraph reading
				Z_gr3_la_tsk_4b_tot Z_gr3_wa_home_total	/// comprehension 
				W3_LT_totscore ///
				using "$tables/learning_bysubtask.tex", replace ///
				tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
				stats(N r2 p1 m,  fmt(0 3 3) labels("Observations" "R-squared" "P-value:Train=Coach" "Control mean")) ///	
				keep( T1 T2) ///
				collabels(none)	 ///
				mgroups("Post-treated (grade 3)" "Directly treated (grade 2)", pattern(1 0 0 0 0 0 1) ///
				prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
				mtitles("Overall ""Letters" "Words" "Paragraph" "Oral compr." "Written compr." "Overall") ///
				nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 								
				
				
				
************************************************************************
* Table 4. Investigating Spillovers|Teacher Outcomes
************************************************************************
	
	use "$input\sustainability_paper_teacher_level.dta", clear
			
	keep if gr3_sample | gr1_sample


foreach var of varlist 	use_lesson_plans_ngo vula_bula phonics_repeat_thrice overall_correct print_me writing_me {
	qui reg `var' T1 T2 $tch_controls i.strata gr3_sample if  exposed, cl(idschool) 
	qui test T1 = T2
	local p1 `r(p)'
	qui sum `var' if treat == 0 & exposed
	local m `r(mean)'
	eststo m_`var', addscalars(p1 `p1' m `m')	
	
	*qui reg `var' T1 T2  i.strata gr3_sample if  spillover_sample, cl(idschool) 
	qui reg `var' T1 T2 $tch_controls i.strata gr3_sample if  exposed == 0, cl(idschool) 
	qui test T1 = T2
	local p1 `r(p)'
	qui sum `var' if treat == 0 & exposed == 0
	local m `r(mean)'
	eststo `var', addscalars(p1 `p1' m `m')	
	
	
	}

	esttab 	 	m_use_lesson_plans_ngo m_vula_bula m_phonics_repeat_thrice m_overall_correct m_print_me m_writing_me   ///
				using "$tables/spillovers.tex", replace ///
				tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
				stats(N r2 p1 m,  fmt(0 3 3 3) labels("Observations" "R-squared" "P-value:Train=Coach" "Control mean")) ///	
				keep( T1 T2 gr3_sample) ///
				collabels(none)	 ///
				nomtitles nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 								


	esttab 	 	use_lesson_plans_ngo vula_bula phonics_repeat_thrice overall_correct print_me writing_me ///
				using "$tables/spillovers.tex", append ///
				tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
				stats(N r2 p1 m,  fmt(0 3 3 3) labels("Observations" "R-squared" "P-value:Train=Coach" "Control mean")) ///	
				keep( T1 T2 gr3_sample) ///
				collabels(none)	 ///
				mtitles("\shortstack{Lesson\\plans}" "\shortstack{Reading\\Booklets}" "Knowledge" "\shortstack{Teaching\\activities}"  /// 
				"\shortstack{Print\\richness}" "Writing") ///
				nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 								

				
********************************
** Table 5. Manually Entered ***
********************************
				
				
		

************************************************************************
* Table A.1. Grade 3 teachers' participation in training. 
************************************************************************

use "$input\sustainability_paper_teacher_level.dta", clear

	lab var received_efal_training_2017 "Received training in 2017"
	lab var received_efal_training_2018 "Received training in 2017"

	
	iebaltab received_training_2017	received_training_2018 received_efal_training_2017 received_efal_training_2018  if gr3_sample & same_gr_sch_2017 == 1, ///
			grpvar(treatment) savet("$tables/Table_A1.tex") replace cov(i.strata) vce(cluster idschool) rowvarlabels  control(0) onerow
			
		
	
****************************
*** Table A.2. Attrition ***
****************************
		
		
	***Panel A. School-level attrition. 
	use "$input\sustainability_paper.dta", clear	 
	duplicates drop idschool cohort, force
	bys idschool: egen tt = mean(cohort)
	gen school_attrite = tt == 2
	duplicates drop idschool, force
	lab var school_attrite "Attrition"
	iebaltab  school_attrite, ///
		grpvar(treatment) savet("$tables/attrition_school_level.tex") replace cov(i.strata)  vce(cluster idschool) rowvarlabels control(0) onerow
		
	***Panel B. Student level attrition (directly treated)
	use "$input\sustainability_paper.dta", clear	 
	keep if cohort == 1
	gen correct_grade_W3 = W3_LT_grade == 2
	lab var  attrite "Attrition"
	lab var W4_attrite "Attrition"
	lab var correct_grade_W3 "Grade 2"
	lab var W4_gr4 "Grade 4"	
	
	iebaltab attrite W4_attrite , ///
		grpvar(treatment) savet("$tables/attrition_student_level.tex") replace cov(i.strata) vce(cluster idschool) rowvarlabels  control(0)  onerow
	
	***Panel C. Grade progression (directly treated)
	iebaltab  correct_grade_W3 W4_gr4, ///
		grpvar(treatment) savet("$tables/grade_progression.tex") replace cov(i.strata)  vce(cluster idschool) rowvarlabels control(0) onerow
		
		
	***Panel D. Baseline balance (non-attriters)
	lab var BL_totscore_SD "Baseline performance"
	iebaltab age_best_W1and2 female_with_miss BL_totscore_SD if cohort == 1 & W4_attrite == 0, ///
		grpvar(treatment) savet("$tables/balance_cohort1_non_attriters.tex") replace cov(i.strata) vce(cluster idschool) rowvarlabels control(0)   balmissreg(groupmean) onerow
		
		
*************************************************
*** Table A.3.Tests for non-random attrition  ***
*************************************************
		
	use "$input\sustainability_paper.dta", clear	 
	keep if cohort == 1
	lab var  attrite "Attrition"
	lab var W4_attrite "Attrition"
	
	foreach var of varlist age_best_W1and2 female_with_miss BL_totscore_SD {
		forvalues i = 1/2 {
			gen `var'xT`i' = `var' * T`i'	
		}
	}	
	
	lab var age_best_W1and2xT1 "Age"
	lab var age_best_W1and2xT1 "Age $\times$ Training" 
	lab var age_best_W1and2xT2 "Age $\times$ Coaching" 
	lab var female_with_miss "Female"
	lab var female_with_missxT1 "Female $\times$ Training"
	lab var female_with_missxT2 "Female $\times$ Coaching"
	lab var BL_totscore_SD "Learning"
	lab var BL_totscore_SDxT1 "Learning $\times$ Training"
	lab var BL_totscore_SDxT2 "Learning $\times$ Coaching"
	
	foreach var of varlist attrite  W4_attrite {
		reg `var' T1 T2 age_best_W1and2* female_with_miss* BL_totscore_SD BL_totscore_SDxT? i.strata, cl(idschool)
		eststo `var'
	}	
	
	esttab 	 	attrite  W4_attrite ///
				using "$tables/non_random_attrition.tex", replace ///
				tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
				stats(N r2,  fmt(0 3) labels("Observations" "R-squared")) ///	
				keep(age_best_W1and2* female_with_miss* BL_totscore_SD BL_totscore_SDxT? T1 T2) ///
				collabels(none)	 ///
				mtitles("Third round" "Fourth round") ///
				nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 								
			

				
				
				
********************
**** Table A.4  ****
********************

	use "$input\sustainability_paper_teacher_level.dta", clear
			
	keep if gr3_sample | gr1_sample



	***Table A.4 Routine
foreach var of varlist correct_* {
	qui reg `var' T1 T2  $tch_controls i.strata if gr3_sample == 1 , cl(idschool) 
	qui test T1 = T2
	local p1 `r(p)'
	qui sum `var' if treat == 0 & gr3_sample
	local m `r(mean)'
	eststo gr3_`var', addscalars(p1 `p1' m `m')		
	
	qui reg `var' T1 T2  $tch_controls i.strata if gr1_sample == 1 , cl(idschool) 
	qui test T1 = T2
	local p1 `r(p)'
	qui sum `var' if treat == 0 & gr1_sample
	local m `r(mean)'
	eststo gr1_`var', addscalars(p1 `p1' m `m')		
	
	qui reg `var' T1 T2  $tch_controls  i.strata gr3_sample if exposed == 1 , cl(idschool) 
	qui test T1 = T2
	local p1 `r(p)'
	qui sum `var' if treat == 0 & exposed
	local m `r(mean)'
	eststo exp_`var', addscalars(p1 `p1' m `m')			

	qui reg `var' T1 T2  $tch_controls i.strata gr3_sample if exposed == 0 , cl(idschool) 
	qui test T1 = T2
	local p1 `r(p)'
	qui sum `var' if treat == 0 & exposed == 0
	local m `r(mean)'
	eststo spi_`var', addscalars(p1 `p1' m `m')				
}

	*Table A.4. Panel A
	esttab 	 	gr3_correct_group_guided gr3_correct_spelling gr3_correct_phonics  gr3_correct_reading gr3_correct_writing ///
				using "$tables/routine.tex", replace ///
				tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
				stats(N p1 m,  fmt(0 3 3) labels("Observations" "P-value:Train=Coach" "Control mean")) ///	
				keep( T1 T2) ///
				collabels(none)	 ///
				mtitles("\shortstack{Group\\guided}" "Spellling" "Phonics" "\shortstack{Shared\\reading}" "Writing") ///
				nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 								
	
	*Table A.4. Panel B
	esttab 	 	gr1_correct_group_guided gr1_correct_spelling gr1_correct_phonics  gr1_correct_reading gr1_correct_writing ///
				using "$tables/routine.tex", append ///
				tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
				stats(N p1 m,  fmt(0 3 3) labels("Observations" "R-squared" "P-value:Train=Coach" "Control mean")) ///	
				keep( T1 T2) ///
				collabels(none)	 ///
				nomtitles nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 						
	*Table A.4. Panel B
	esttab 	 	exp_correct_group_guided exp_correct_spelling exp_correct_phonics exp_correct_reading exp_correct_writing ///
				using "$tables/routine.tex", append ///
				tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
				stats(N p1 m,  fmt(0 3 3) labels("Observations" "R-squared" "P-value:Train=Coach" "Control mean")) ///	
				keep( T1 T2) ///
				collabels(none)	 ///
				nomtitles nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 						

	esttab 	 	spi_correct_group_guided spi_correct_spelling spi_correct_phonics spi_correct_reading spi_correct_writing ///
				using "$tables/routine.tex", append ///
				tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
				stats(N p1 m,  fmt(0 3 3) labels("Observations" "R-squared" "P-value:Train=Coach" "Control mean")) ///	
				keep( T1 T2) ///
				collabels(none)	 ///
				nomtitles nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 						
						

					
********************
**** Table A.5  ****
********************					
	

foreach var of varlist print_me W4_TQ_rate_i W4_TQ_rate_ii W4_TQ_rate_iii W4_TQ_rate_vi W4_TQ_rate_ix { //Grade 3
	qui reg `var' T1 T2  $tch_controls i.strata if gr3_sample == 1 , cl(idschool) 
	qui test T1 = T2
	local p1 `r(p)'
	qui sum `var' if treat == 0 & gr3_sample
	local m `r(mean)'
	eststo gr3_`var', addscalars(p1 `p1' m `m')		
	
	qui reg `var' T1 T2  $tch_controls i.strata if gr1_sample == 1 , cl(idschool)  //Grade 1
	qui test T1 = T2
	local p1 `r(p)'
	qui sum `var' if treat == 0 & gr1_sample
	local m `r(mean)'
	eststo gr1_`var', addscalars(p1 `p1' m `m')		
}

	esttab 	 	gr3_W4_TQ_rate_i gr3_W4_TQ_rate_ii gr3_W4_TQ_rate_iii gr3_W4_TQ_rate_vi gr3_W4_TQ_rate_ix ///
				using "$tables/print_rich.tex", replace ///
				tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
				stats(N,  fmt(0 3) labels("Observations")) ///	
				keep( T1 T2) ///
				collabels(none)	 ///
				mtitles("\shortstack{Reading\\corner}" "\shortstack{Story\\books}"  "\shortstack{Setswana\\wall charts}" "\shortstack{Picture\\wall charts}"  /// 
				"\shortstack{Flash\\cards}") ///
				nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 								
		
	esttab 	 	gr1_W4_TQ_rate_i gr1_W4_TQ_rate_ii gr1_W4_TQ_rate_iii gr1_W4_TQ_rate_vi gr1_W4_TQ_rate_ix ///
				using "$tables/print_rich.tex", append ///
				tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
				stats(N,  fmt(0) labels("Observations")) ///	
				keep( T1 T2) ///
				collabels(none)	 ///
				nomtitles nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 						

foreach var of varlist W4_TQ_rate_i W4_TQ_rate_ii W4_TQ_rate_iii W4_TQ_rate_vi W4_TQ_rate_ix {
	qui reg `var' T1 T2  $tch_controls i.strata gr3_sample  if exposed == 1 , cl(idschool)  // Exposed
	qui test T1 = T2
	local p1 `r(p)'
	qui sum `var' if treat == 0 & exposed
	local m `r(mean)'
	eststo `var', addscalars(p1 `p1' m `m')			
}
	esttab 	 	W4_TQ_rate_i W4_TQ_rate_ii W4_TQ_rate_iii W4_TQ_rate_vi W4_TQ_rate_ix ///
				using "$tables/print_rich.tex", append ///
				tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
				stats(N,  fmt(0) labels("Observations")) ///	
				keep( T1 T2) ///
				collabels(none)	 ///
				nomtitles nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 						
				
foreach var of varlist W4_TQ_rate_i W4_TQ_rate_ii W4_TQ_rate_iii W4_TQ_rate_vi W4_TQ_rate_ix {
	qui reg `var' T1 T2  $tch_controls i.strata gr3_sample  if exposed == 0 , cl(idschool)  //spillover
	qui test T1 = T2
	local p1 `r(p)'
	qui sum `var' if treat == 0 & exposed == 0
	local m `r(mean)'
	eststo `var', addscalars(p1 `p1' m `m')			
}
	esttab 	 	W4_TQ_rate_i W4_TQ_rate_ii W4_TQ_rate_iii W4_TQ_rate_vi W4_TQ_rate_ix ///
				using "$tables/print_rich.tex", append ///
				tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
				stats(N,  fmt(0) labels("Observations")) ///	
				keep( T1 T2) ///
				collabels(none)	 ///
				nomtitles nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 						

			
				
********************
**** Table A.6  ****
********************	  

foreach var of varlist W4_TQ_days_exercises W4_TQ_days_writing  W4_TQ_days_sentence W4_TQ_days_paragraph   {
	qui reg `var' T1 T2  $tch_controls i.strata if gr3_sample == 1 , cl(idschool) 
	eststo gr3_`var'
	
	qui reg `var' T1 T2   $tch_controls i.strata if gr1_sample == 1 , cl(idschool) 
	eststo gr1_`var'
}
	*Panel A
	esttab 	 	gr3_W4_TQ_days_exercises gr3_W4_TQ_days_writing  gr3_W4_TQ_days_sentence gr3_W4_TQ_days_paragraph   ///
				using "$tables/writing.tex", replace ///
				tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
				keep( T1 T2) ///
				collabels(none)	 ///
				mtitles("Any exercise" "Writing" "Full sentence" "Paragraph" "No. pages") ///
				nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 								
	
	*Panel B
	
	esttab 	 	gr1_W4_TQ_days_exercises gr1_W4_TQ_days_writing  gr1_W4_TQ_days_sentence gr1_W4_TQ_days_paragraph  ///
				using "$tables/writing.tex", append ///
				tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
				keep( T1 T2) ///
				collabels(none)	 ///
				nomtitles nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 						

foreach var of varlist W4_TQ_days_exercises W4_TQ_days_writing  W4_TQ_days_sentence W4_TQ_days_paragraph  {
	qui reg `var' T1 T2  $tch_controls i.strata if exposed == 1 , cl(idschool) 
	eststo `var'

}
	*Panel C

	esttab 	 	W4_TQ_days_exercises W4_TQ_days_writing  W4_TQ_days_sentence W4_TQ_days_paragraph   ///
				using "$tables/writing.tex", append ///
				tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
				keep( T1 T2) ///
				collabels(none)	 ///
				nomtitles nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 						

	*Panel D
foreach var of varlist W4_TQ_days_exercises W4_TQ_days_writing  W4_TQ_days_sentence W4_TQ_days_paragraph  {
	qui reg `var' T1 T2  $tch_controls  i.strata if exposed == 0 , cl(idschool) 
	eststo `var'

}
	esttab 	 	W4_TQ_days_exercises W4_TQ_days_writing  W4_TQ_days_sentence W4_TQ_days_paragraph   ///
				using "$tables/writing.tex", append ///
				tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
				keep( T1 T2) ///
				collabels(none)	 ///
				nomtitles nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 						
				
				
	
************************************************************************
* Table A.7. Sensitivity checks
************************************************************************
	use "$input\sustainability_paper.dta", clear
	keep if cohort == 2
	keep if W4_LT_gr3_totscore ~= .
	*Create dummies from strata indicator variable. 
	tab strata, gen(dum_strata)
	drop dum_strata10
	
	*column (2). Same controls, not interacted. 
	qui reg W4_LT_gr3_totscore T1 T2  $gr3_controls i.strata, cl(idschool) 
		qui test T1 = T2
		local p1 `r(p)'
		eststo not_interacted, addscalars(p1 `p1')	
	
	**Column 3. LAsso regression. First select the control variables then run the regression on the selected variables. 
	cvlasso W4_LT_gr3_totscore Learner_Age w4_female C2011_school_att_13_18yr district  ANA_new  mean_BL*  ///
        T1 T2 dum_strata? ///
        , partial(T1 T2 dum_strata?) ///
        maxiter(200) nf(10) seed(99) prestd plotcv 
		
    lasso2 W4_LT_gr3_totscore Learner_Age w4_female C2011_school_att_13_18yr district  ANA_new  mean_BL*  ///
        T1 T2 dum_strata? ///
        , partial(T1 T2 dum_strata?) ///
        maxiter(2000) lambda(`e(lopt)') // Running at optimal lambda. 
	global selected `e(selected)'
	
	
	qui reg W4_LT_gr3_totscore T1 T2 i.strata $selected, cl(idschool)
	qui test T1 = T2
		local p1 `r(p)'
		eststo pds_not_interacted, addscalars(p1 `p1')	
	
	
	
	**Column 4: Original specification, but dropping overage. 
	*To drop over-age we first need to demean relative a new mean (when the sample is different)
	cap drop z*
	foreach var of varlist $gr3_controls dum_str*  {
	qui sum `var'  if Learner_Age < 11
	gen z_`var' = `var' - `r(mean)'  if Learner_Age < 11
	forvalues i = 1/2 {
		gen z_`var'_x_T`i' = z_`var' * T`i'  if Learner_Age < 11
	}	
}	
	qui reg W4_LT_gr3_totscore T1 T2 z* if Learner_Age < 11, cl(idschool)
		qui test T1 = T2
		local p1 `r(p)'
		eststo no_overage, addscalars(p1 `p1')	
	
	esttab 	 	W4_LT_gr3_totscore  not_interacted  pds_not_interacted no_overage ///
				using "$tables/robustness.tex", replace ///
				tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
				stats(N r2 p1,  fmt(0 3 3 0) labels("Observations" "R-squared" "P-value:Train=Coach" "Control mean")) ///	
				keep( T1 T2) ///
				collabels(none)	 ///
				nomtitles nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 								


************************************************************************
* Table A.8. Treatment effects for the directly treated cohort, by round of data collection.
************************************************************************

	use "$input\sustainability_paper.dta", clear
	keep if cohort == 1
	keep if W3_LT_totscore ~= .
	drop if W4_attrite == 1

	tab strata, gen(dum_strata)
	drop dum_strata10
	**Grade 2
	foreach var of varlist  $cohort1_controls dum_str* {
	qui sum `var' 
	gen z_`var' = `var' - `r(mean)' 
	forvalues i = 1/2 {
		gen z_`var'_x_T`i' = z_`var' * T`i'
	}	
	}
		
	**Wave 3 (grade 2)		
	qui reg W3_LT_totscore T1 T2 z_*, cl(idschool)
		qui test T1 = T2
		local p1 `r(p)'
		qui sum `var' if treat == 0 
		local m `r(mean)'
		eststo gr2_noattriters, addscalars(p1 `p1' m `m')	
		
	**Grade 2
	keep if W2_LT_totscore ~= .
	drop z*
	foreach var of varlist  $cohort1_controls dum_str* {
	qui sum `var' 
	gen z_`var' = `var' - `r(mean)' 
	forvalues i = 1/2 {
		gen z_`var'_x_T`i' = z_`var' * T`i'
	}	
	}		
	**Wave 2 (grade 1)
	qui reg W2_LT_totscore T1 T2 z*, cl(idschool)
		qui test T1 = T2
		local p1 `r(p)'
		qui sum `var' if treat == 0 
		local m `r(mean)'
		eststo gr1_noattriters, addscalars(p1 `p1' m `m')			

	esttab 	 	W2_LT_totscore gr1_noattriters W3_LT_totscore  gr2_noattriters ///
				using "$tables/sensitive_attrition.tex", replace ///
				tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
				stats(N r2 p1,  fmt(0 3 3 0) labels("Observations" "R-squared" "P-value:Train=Coach" "Control mean")) ///	
				keep( T1 T2) ///
				collabels(none)	 ///
				mgroups("\shortstack{Round 2\\(End of Grade 1)}" "\shortstack{Round 3\\(End of Grade 2)}", pattern(1 0 1 0) ///
				prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
				mtitles("" "" "" "") ///
				nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 	
				
	
/********************************************************
*** Table A.9: Balance Exposed and spillover teachers ***
********************************************************/
	use "$input\sustainability_paper_teacher_level.dta", clear	
	*gen recent_teacher = years_at_school <= 3 //These teachers joined after the start of the program. 
	lab var recent_teacher "Recent teacher (previous 3 years)"			
	lab var same_gr_sch_2017 "Taught grade 3 2017"
	keep if gr3_sample | gr1_sample
	lab var exposed "Previously exposed"
	
	***Panel A. Exposed teachers
	iebaltab female W4_TQ_tage	W4_TQ_yearsteach W4_TQ_multigrade W4_TQ_learnerno years_at_school recent_teacher if exposed, ///
			grpvar(treatment) savet("$tables/balance_teacher_exposed.tex") replace cov(i.strata) vce(cluster idschool) ///
			balmissreg(groupmean) onerow rowvarlabels  control(0) 
	***Panel B. Spillover teachers
	iebaltab female W4_TQ_tage	W4_TQ_yearsteach W4_TQ_multigrade W4_TQ_learnerno years_at_school recent_teacher if exposed == 0, ///
			grpvar(treatment) savet("$tables/balance_teacher_spillovers.tex") replace cov(i.strata) vce(cluster idschool) ///
			rowvarlabels  control(0) onerow balmissreg(groupmean) 
	